How to use a keypress event in AngularJS?
How to use a keypress event in AngularJS?
27417-Jun-2024
Updated on 19-Jun-2024
Home / DeveloperSection / Forums / How to use a keypress event in AngularJS?
How to use a keypress event in AngularJS?
Ashutosh Kumar Verma
19-Jun-2024Keypress Event
In AngularJS, you can use the built-in directive
ng-keypress
to handle the keypress event.Here are the steps to use this event,
HTML
To add the keypress event in the angular application, you can use the
ng-keypress
as given below,In the above syntax
ng-keypress
is the built-in directive that triggers thehandleKeypress
function when a key/button is pressed.$event
is the special type of angular expression that passes the event object to the function.JavaScript
Here's how to control for keypress events in the Angular JS application,
handleKeypress
is the function defined in the controller scope that is called every time the key/button is pressed.event
is the event object that contains the information about the keypress(event.key)
event.Example-
let's take a simple example that explain the use of keypress event in angular js,
<input type="text" ng-keypress="handleKeypress($event)">
: The input field that triggers thehandleKeypress
function every time when a key is pressed.Output-
Also, Read: How to display length of filtered ng-repeat data?